package com.kitty.game.rank.service.handler;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.rank.message.RespRankTopUser;
import com.kitty.game.rank.model.PersonInfo;
import com.kitty.game.rank.model.Range;
import com.kitty.game.rank.model.record.RankRecord;
import com.kitty.game.rank.model.record.SpeedRankRecord;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.model.RoleSimple;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**速度排行榜处理类*/
@Component
public class SpeedRankHandler extends RangeRankHandler {
    private List<Range<Short>> levelRanges = new ArrayList<>();
    /**初始化等级段*/
    @PostConstruct
    private void initRange() {
        levelRanges.add(new Range((short) 70, (short) 79));
        levelRanges.add(new Range((short) 80, (short) 89));
        levelRanges.add(new Range((short) 90, (short) 99));
        levelRanges.add(new Range((short) 100, (short) 109));
        levelRanges.add(new Range((short) 110, (short) 119));
        levelRanges.add(new Range((short) 120, (short) 129));
        levelRanges.add(new Range((short) 130, (short) 139));
        levelRanges.add(new Range((short) 140, (short) 149));
        levelRanges.add(new Range((short) 150, (short) 159));
        levelRanges.add(new Range((short) 160, (short) 169));
        levelRanges.add(new Range((short) 170, (short) 179));
    }
    @Override
    public void init() {
        for (Range<Short> range : levelRanges) {
            init(range);
        }
    }
    private void init(Range<Short> range) {
        short min = range.getMin();
        short max = range.getMax();
        Dao dao = SpringUtils.getBean(Dao.class);
        List<RoleSimple> roles = dao.query(RoleSimple.class, Cnd.where("level", ">=", min).and("level", "<=", max).desc("speed"), dao.createPager(1, getRankSize()));
        for (RoleSimple role : roles) {
            if(role.isAi(role)){
                continue;
            }
            String levelRange = getLevelRange(range);
            this.update(levelRange, role.getGid(), new SpeedRankRecord(role.getGid(), role.getSpeed(), role.getPartyName(), role.getName(), role.getPolar(), role.getRoleIcon(), role.getLevel()));
        }
    }
    private String getLevelRange(Range range) {
        return range.getMin() + "-" + range.getMax()+"-105";
    }

    /**找到角色对应的分段*/
    public String getLevelRange(Role role) {
        short roleLevel = role.getLevel();
        Range<Short> range = getRange(roleLevel);
        if (range != null) {
            return getLevelRange(range);
        }
        return null;
    }

    private Range getRange(short roleLevel) {
        for (Range<Short> range : levelRanges) {
            if (roleLevel >= range.getMin() && roleLevel <= range.getMax()) {
                return range;
            }
        }

        return null;
    }
    public RespRankTopUser getRankResponse(short type, String range) {
        RespRankTopUser respRankTopUser = new RespRankTopUser();
        respRankTopUser.setType(type);
        respRankTopUser.setCookie(new Long(System.currentTimeMillis() / 1000).intValue());
        respRankTopUser.setList(new ArrayList<>());
        List<PersonInfo> personInfoList = getPersonInfoList(getRecordsFrom(range));
        respRankTopUser.setList(personInfoList);
        respRankTopUser.setSize((short) respRankTopUser.getList().size());
        String[] levels = range.split("-");
        respRankTopUser.setMin(Short.parseShort(levels[0]));
        respRankTopUser.setMax(Short.parseShort(levels[1]));
        return respRankTopUser;
    }
    public List<RankRecord> getRecordsFrom(String range) {
        return getRecordsFrom(range, 0, getRankSize());
    }

    public List<PersonInfo> getPersonInfoList(List<RankRecord> records) {
        List<PersonInfo> list = new ArrayList<>(records.size());
        for (RankRecord rankRecord : records) {
            SpeedRankRecord speedRankRecord = (SpeedRankRecord)rankRecord;
            PersonInfo personInfo = new PersonInfo();
            personInfo.setList(new ArrayList<>());
            personInfo.getList().add(new FiedValue(305, speedRankRecord.getGid()));
            personInfo.getList().add(new FiedValue(855, speedRankRecord.getPartyName()));
            personInfo.getList().add(new FiedValue(1, speedRankRecord.getName()));
            personInfo.getList().add(new FiedValue(14, speedRankRecord.getSpeed()));
            personInfo.getList().add(new FiedValue(44, speedRankRecord.getPolar()));
            personInfo.getList().add(new FiedValue(40, speedRankRecord.getRoleIcon()));
            personInfo.getList().add(new FiedValue(31, speedRankRecord.getLevel()));
            list.add(personInfo);
        }
        return list;
    }
    /**角色升级跨等级段处理*/
    public void handleRoleUpgrade(Role role, short oldLevel, short newLevel) {
        /**判断角色是否跨等级段，是的话则从旧等级段排行榜删除，加入新等级段排行榜*/

        Range oldRange = getRange(oldLevel);
        Range newRange = getRange(newLevel);
        /**同一等级段时，返回*/
        if (oldRange != null && oldRange.equals(newRange)) {return ;}

        if (oldRange != null) {
            String oldLevelRange = getLevelRange(oldRange);
            remove(oldLevelRange, role.getGid());
        }

        if (newRange != null) {
            String newLevelRange = getLevelRange(newRange);
            this.update(newLevelRange, role.getGid(), new SpeedRankRecord(role.getGid(), role.getSpeed(), role.getPartyName(), role.getName(), role.getPolar(), role.getRoleIcon(), role.getLevel()));
        }
    }
}
